﻿
using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using Core.Library;
using DataObject;
using DataObject.Members;
using DataObject.Ref;
using DataObject.Vote;

namespace Event.Controls.Album
{
    public partial class Infor : System.Web.UI.UserControl
    {
        #region variables, constants, properties
        private const int _imageWidth = 80;
        private const int _imageHeight = 80;
        private const int _imageLightBoxWidth = 130;
        private const int _imageLightBoxHeight = 130;
        private const string IMG_FORMAT = @"<img id=""detail-avatar"" alt=""{1}"" title=""{1}"" src=""{0}"" />";
        private const string EDIT = @"<a class=""detail-edit"" href=""/sua-thong-tin.htm"">CHỈNH SỬA BÀI THI</a>";
        private const string VOTE_BUTTON = @"<a id=""detail-vote-btn"" uid=""{3}"" vote=""{2}"" class=""{1} d-vote"" href=""javascript:void(0)"" {0} >VOTE{4}</a>";
        protected string _link;
        protected string _script = string.Empty;
        #endregion

        internal bool DataBind(DataTable dt, int round)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var r = dt.Rows[0];
                var emailShort = Utils.GetIdFromRequestString(Utils.Email, string.Empty);
                if (emailShort != Utils.CutEmail(r[Constants.Table_Members.Member_Email].ToString()))
                    Channelvn.Cached.UrlRewrite._301Redirection._301Redirect(r[Constants.Table_Members.Url].ToString());
                var vote = VoteCache.Members_GetVoteByRound(ConvertUtility.ConvertTo<int>(r[Constants.Table_Members.Member_ID]), round);
                var isOtherAvatar = r[Constants.Table_Members.Member_Avatar].ToString().StartsWith("http");

                var avatar = isOtherAvatar ? r[Constants.Table_Members.Member_Avatar].ToString() : Utils.Photo.GetThumbImage_Zoom(
                    r[Constants.Table_Members.Member_Avatar].ToString(),
                    _imageWidth,
                    _imageHeight);
                var avatarLightBox = isOtherAvatar ? r[Constants.Table_Members.Member_Avatar].ToString() : Utils.Photo.GetThumbImage_Zoom(
                    r[Constants.Table_Members.Member_Avatar].ToString(),
                    _imageLightBoxWidth,
                    _imageLightBoxHeight);
                var avatarMeta = isOtherAvatar ? r[Constants.Table_Members.Member_Avatar].ToString() : Utils.Photo.GetThumbImage_Zoom(
                    r[Constants.Table_Members.Member_Avatar].ToString(),
                    200,
                    200);

                MetaKeyword.AddMetaTag(Page, string.Format("{0} | {1}", r[Constants.Table_Members.Member_FullName], Const.Meta_Title), string.IsNullOrWhiteSpace(r[Constants.Table_Members.Member_About].ToString()) ? Const.Meta_Sapo : r[Constants.Table_Members.Member_About].ToString());
                MetaKeyword.AddFacebookMetaTag(Page, string.Format("{0} | {1}", r[Constants.Table_Members.Member_FullName], Const.Meta_Title), string.IsNullOrWhiteSpace(r[Constants.Table_Members.Member_About].ToString()) ? Const.Meta_Sapo : r[Constants.Table_Members.Member_About].ToString(), r[Constants.Table_Members.Url].ToString(), avatarMeta);

                ltrAvatar.Text = string.Format(IMG_FORMAT, avatar, r[Constants.Table_Members.EncodeName]);
                ltrFullName.Text = r[Constants.Table_Members.Member_FullName].ToString();
                ltrVote.Text = vote.ToString();
                ltrAge.Text = r[Constants.Table_Members.Age].ToString();
                ltrCity.Text = r[Constants.Table_City.City_Name].ToString();
                ltrAbout.Text = r[Constants.Table_Members.Member_About].ToString();
                hfAvatar.Value = avatarLightBox;
                hfFullName.Value = r[Constants.Table_Members.Member_FullName].ToString();
                hfVote.Value = r[Constants.Table_Members.Vote].ToString();
                hfUrl.Value = r[Constants.Table_Members.Url].ToString();
                _link = r[Constants.Table_Members.Url].ToString();
                var isLockVote = RefCache.Ref_GetLockVoteStatus();
                var currentRound = RefCache.Ref_GetCurrentRound();
                var displayVote = RefCache.Ref_GetVoteDisplayStatus();
                if (isLockVote && round == currentRound)
                {
                    hfLockVote.Value = "1";
                    _script = String.Format("onclick = \"Vote.voteMember({0});\"", r[Constants.Table_Members.Member_ID]);
                }
                if (displayVote || round < currentRound)
                {
                    hfVoteDisplay.Value = "1";
                }
                hfEncodedBase64Title.Value = r[Constants.Table_Members.EncodedBase64Title].ToString();

                var clsVote = string.Empty;
                var textVote = string.Empty;
                string _Email = string.Empty;
                if (HttpContext.Current.Request.IsAuthenticated)
                {
                    var ident = HttpContext.Current.User.Identity as FormsIdentity;
                    if (ident != null)
                    {
                        try
                        {
                            FormsAuthenticationTicket ticket1 = ident.Ticket;
                            string[] userData = ticket1.UserData.Split('|');
                            _Email = userData[1];
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                else if (HttpContext.Current.Session[Const.AuthenticationSessionName] != null)
                {
                    string[] userData = HttpContext.Current.Session[Const.AuthenticationSessionName].ToString().Split('|');
                    _Email = userData[1];
                }

                if (!string.IsNullOrEmpty(_Email))
                {
                    if (_Email == r[Constants.Table_Members.Member_Email].ToString() && RefCache.Ref_GetLockEditStatus()
                && ConvertUtility.ConvertTo<int>(r[Constants.Table_Members.Member_InRound].ToString()) >= currentRound)
                    {
                        ltrEdit.Text = EDIT;
                    }

                    var from = MembersCache.Members_GetByEmail(_Email);
                    var lstVoted = VoteCache.Vote_GetVoteLogByFromID(from.Member_ID);
                    if (lstVoted != null && lstVoted.Where(d => d.VoteLog_ToID == ConvertUtility.ConvertTo<int>(r[Constants.Table_Members.Member_ID])).Count() > 0)
                    {
                        hfLockVote.Value = "0";
                        _script = string.Empty;
                        clsVote = isLockVote ? "active" : string.Empty;
                        textVote = isLockVote ? "D" : string.Empty;
                    } 
                }

                ltrVoteBtn.Text = string.Format(VOTE_BUTTON, _script, clsVote, vote, r[Constants.Table_Members.Member_ID], textVote);
                return true;
            }
            return false;
        }
    }
}